package com.controller;

import com.bean.User;
import com.dao.JDBC;
import com.service.RegisterService;
import com.service.impl.RegisterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/com/controller/ServletRegister")
public class ServletRegister extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        RegisterService registerService = new RegisterServiceImpl();
        boolean flag = registerService.CheckService(user);
        if (!flag&&!username.equals("")&&!password.equals("")){
            registerService.InsertService(user);
            request.getRequestDispatcher("/RegisterSuccess.jsp").forward(request,response);
        }
        else{
            if (username.equals("")||password.equals("")){
                request.setAttribute("ERROR","用户名或密码为空");
            }else {
                request.setAttribute("ERROR","用户名已经存在");
            }
            request.getRequestDispatcher("/RegisterLoss.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
